//
//  LoginViewController.m
//  DuckAndDoc
//
//  Created by 蓝水软件 on 2017/5/10.
//  Copyright © 2017年 苏州蓝水有限公司. All rights reserved.
//

#import "LoginViewController.h"
#import "MainViewController.h"
#import "LeftDrawerViewController.h"

@interface LoginViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) UIImageView *backImage;   //背景图片
@property (nonatomic, strong) UIImageView *headImage;   //头像
@property (nonatomic, strong) UITextField *userName;    //用户名
@property (nonatomic, strong) UITextField *passWord;    //密码输入框
@property (nonatomic, strong) UIView *line;             //文本框下方横线
@property (nonatomic, strong) UIButton *loginButton;    //登录按钮
@property (nonatomic, strong) UIButton *changeButtonType; //更换按钮类型
@property (nonatomic, strong) NSMutableDictionary *textStyle;
@end

#define BackImageName [UIImage imageNamed:@"login_back"]
#define headImageName [UIImage imageNamed:@"lanzi"]

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}

- (void)setUI{
    self.view.backgroundColor = [UIColor flatBlackColor];
    [self.view addSubview:self.backImage];
    [self.view addSubview:self.headImage];
    [self.headImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(120);
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(88, 88));
    }];
    CGFloat left = 32;
    CGFloat height = 40;
    //用户名
    [self.view addSubview:self.userName];
    [self.userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headImage.mas_bottom).offset(60);
        make.left.mas_equalTo(left);
        make.right.mas_equalTo(-left);
        make.height.mas_equalTo(height);
    }];
    //密码
    [self.view addSubview:self.passWord];
    [self.passWord mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_userName.mas_bottom).offset(48);
        make.left.mas_equalTo(left);
        make.right.mas_equalTo(-left);
        make.height.mas_equalTo(height);
    }];
    //登录按钮
    [self.view addSubview:self.loginButton];
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_passWord.mas_bottom).offset(80);
        make.left.mas_equalTo(64);
        make.right.mas_equalTo(-64);
        make.height.mas_equalTo(40);
    }];
}

- (UIView *)leftViewWithImage:(NSString *)imageName{
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    UIImageView *leftImage = [[UIImageView alloc] init];
    leftImage.image = [UIImage imageNamed:imageName];
    [leftView addSubview:leftImage];
    [leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(8);
        make.size.mas_equalTo(CGSizeMake(24, 24));
        make.centerY.equalTo(leftView);
    }];
    return leftView;
}

//文本框下方的横线
//（并非懒加载）
- (UIView *)line{
    _line = [[UIView alloc]init];
    _line.backgroundColor = [UIColor flatWhiteColor];
    return _line;
}

#pragma mark -- buttonAction
//跳转到主界面，如果成功的话并给主界面的isLogin赋值，赋值为YES，否则return
- (void)login{
    if (_userName.text.length == 0) {
        
        [QXToast showTopWithText:LocalizedStrFrom(@"nonUserNameOfChina")];
        [[LxxPlaySound sharePlaySound] play];
        return;
    }
    if (_passWord.text.length == 0) {
        [[LxxPlaySound sharePlaySound] play];
        [QXToast showTopWithText:LocalizedStrFrom(@"nonPassWordOfChina")];
        return;
    }
    if (_passWord.text.length < 6) {
        [[LxxPlaySound sharePlaySound] play];
        [QXToast showTopWithText:LocalizedStrFrom(@"passWordLenghtThanSixOfChina")];
        return;
    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [QXApi loginInbackgroundWithAccount:_userName.text passWord:_passWord.text block:^(BmobUser *user, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (error ) {
            [[LxxPlaySound sharePlaySound] play];
            [QXToast showTopWithText:NSLocalizedString(@"loginFaileResonOfChina", nil)];
        }else{
            [self pushToMainViewController];
        }
    }];
}

- (void)pushToMainViewController{
    MainViewController *centerController = [[MainViewController alloc] init];
    CustomNavigationViewController *centerNavigationController = [[CustomNavigationViewController    alloc] initWithRootViewController:centerController];
    LeftDrawerViewController *leftDrawerController = [[LeftDrawerViewController alloc] init];
    leftDrawerController.delegate = centerController;
    MMDrawerController *drawerViewController = [[MMDrawerController alloc] initWithCenterViewController:centerNavigationController leftDrawerViewController:leftDrawerController];
 
    [drawerViewController setRestorationIdentifier:@"MMDrawer"];
    [drawerViewController setMaximumRightDrawerWidth:160.0];
    [drawerViewController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    [drawerViewController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
    drawerViewController.showsShadow = YES;
    [self presentViewController:drawerViewController animated:YES completion:nil];
}

#pragma mark textFieldDelagate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (_userName.text.length > 0 && _passWord.text.length >= 6) {
        _loginButton.backgroundColor = [UIColor flatMintColor];
        _loginButton.layer.borderWidth = 0;
        [_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
    }else{
        _loginButton.backgroundColor = [UIColor clearColor];
        _loginButton.layer.borderWidth = 1;
        _loginButton.layer.borderColor = [UIColor flatWhiteColor].CGColor;
        [_loginButton setTitleColor:FlatGray forState:UIControlStateNormal];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField == _userName) {
        [_passWord becomeFirstResponder];
    }else{
        [textField resignFirstResponder];
        [self login];
    }
    return YES;
}

#pragma mark -- 懒加载
//用户名
- (UITextField *)userName{
    if (_userName == nil) {
        _userName = [[UITextField alloc]init];
        _userName.backgroundColor = [UIColor clearColor];
        _userName.textColor = [UIColor whiteColor];
        _userName.delegate = self;
        _userName.returnKeyType = UIReturnKeyNext;
        _userName.clearButtonMode = UITextFieldViewModeWhileEditing;
        _userName.leftView = [self leftViewWithImage:@"user_login"];
        _userName.leftViewMode = UITextFieldViewModeAlways;
        _userName.font = [UIFont systemFontOfSize:15];
        NSAttributedString *userNamePlaceholder = [[NSAttributedString alloc] initWithString:LocalizedStrFrom(@"userNameOfChina") attributes:self.textStyle];
        [_userName setAttributedPlaceholder:userNamePlaceholder];
        [_userName addSubview:self.line];
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(40);
            make.right.mas_equalTo(0);
            make.height.mas_equalTo(0.5);
            make.bottom.mas_equalTo(-1);
        }];
    }
    return _userName;
}

//密码
- (UITextField *)passWord{
    if (_passWord == nil) {
        _passWord = [[UITextField alloc]init];
        _passWord.delegate = self;
        _passWord.backgroundColor = [UIColor clearColor];
        _passWord.textColor = [UIColor flatWhiteColor];
        _passWord.leftView = [self leftViewWithImage:@"passWord_login"];
        _passWord.leftViewMode = UITextFieldViewModeAlways;
        _passWord.clearButtonMode = YES;
        _passWord.secureTextEntry = YES;
        _passWord.returnKeyType = UIReturnKeyGo;
        _passWord.font = [UIFont systemFontOfSize:15];
        NSAttributedString *passWordPlaceholder = [[NSAttributedString alloc] initWithString:LocalizedStrFrom(@"passWordOfChina") attributes:self.textStyle];
        [_passWord setAttributedPlaceholder:passWordPlaceholder];
        [_passWord addSubview:self.line];
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(40);
            make.right.mas_equalTo(0);
            make.height.mas_equalTo(0.5);
            make.bottom.mas_equalTo(-1);
        }];
    }
    return _passWord;
}

//文本样式
- (NSMutableDictionary *)textStyle{
    if (_textStyle == nil) {
        _textStyle =  [NSMutableDictionary dictionary];
        _textStyle[NSForegroundColorAttributeName] = [UIColor flatWhiteColor];
    }
    return _textStyle;
}

//背景图片
- (UIImageView *)backImage{
    if (_backImage == nil) {
        _backImage = [[UIImageView alloc] init];
        _backImage.image = [BackImageName imageByBlurDark] ;
        _backImage.frame = [UIScreen mainScreen].bounds;
    }
    return _backImage;
}

//头像
- (UIImageView *)headImage{
    if (_headImage == nil) {
        CGFloat headImageWidth = 88;
        _headImage = [[UIImageView alloc] initWithImage:headImageName];
        _headImage.backgroundColor = [UIColor clearColor];
        _headImage.layer.cornerRadius = headImageWidth/2;
        _headImage.layer.masksToBounds = YES;
    }
    return _headImage;
}

//登录按钮
- (UIButton *)loginButton{
    if (_loginButton == nil) {
        _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginButton setTitle:LocalizedStrFrom(@"loginOfChina") forState:UIControlStateNormal];
        _loginButton.backgroundColor = [UIColor clearColor];
        _loginButton.layer.borderWidth = 1;
        _loginButton.layer.borderColor = [UIColor flatWhiteColor].CGColor;
        _loginButton.layer.cornerRadius = 5;
        [_loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
        [_loginButton setTitleColor:FlatGray forState:UIControlStateNormal];
        [_loginButton setTitleColor:FlatRed forState:UIControlStateHighlighted];
    }
    return _loginButton;
}
@end

